/**
 * list深度拷贝否则数据会污染
 */
export const deepCopy = (obj) => {
    let thisobj = obj;
    if (typeof thisobj === 'object') {
        thisobj = JSON.parse(JSON.stringify(thisobj));
    }
    return thisobj;
};

export class RandsReg {
    constructor(
        conf = {
            num: 0
        }
    ) {
        this.getimage = conf.getimage;
        this.num = conf.num;
        this.str = null;
        this.canvas = document.createElement('canvas');
        this.width = 167;
        this.height = 75;
    }
    reload(getimage = (res) => {}) {
        this.canvas.width = this.width;
        this.canvas.height = this.height;
        this.ctx = this.canvas.getContext('2d');
        this.str = this.rands(this.num);
        this.ctx.fillStyle = this.randColor(180, 230);
        this.ctx.fillRect(0, 0, this.width, this.height);
        this.strStyle();
        this.randPoin();
        getimage(this.canvas.toDataURL('image/png'));
        // document.body.appendChild(this.canvas)
    }
    regRangs() {
        return this.str;
    }
    rands(num = 4) {
        let str = Math.random().toString(36).substr(2);
        if (str.length >= num) {
            return str.substr(0, num);
        }
        str += random(num - str.length);
        return str;
    }
    randColor(min, max) {
        let r = this.rn(min, max);
        let g = this.rn(min, max);
        let b = this.rn(min, max);
        return `rgb(${r},${g},${b})`;
    }
    strStyle() {
        let arr = this.str.split('');
        for (let i = 0, j = arr.length; i < arr.length; i++) {
            let fs = this.rn(65, 70);
            let deg = this.rn(-30, 30); //字体的旋转角度
            this.ctx.font = fs + 'px Simhei';
            this.ctx.textBaseline = 'top';
            this.ctx.fillStyle = this.randColor(80, 150);
            this.ctx.save();
            this.ctx.translate(30 * i + 15, 15);
            this.ctx.rotate((deg * Math.PI) / 180);
            this.ctx.fillText(arr[i], -15 + 5, -15);
            this.ctx.restore();
        }
    }
    randLine() {
        for (var i = 0; i < 5; i++) {
            this.ctx.beginPath();
            this.ctx.moveTo(this.rn(0, this.width), this.rn(0, this.height));
            this.ctx.lineTo(this.rn(0, this.width), this.rn(0, this.height));
            this.ctx.strokeStyle = this.randColor(180, 230);
            this.ctx.closePath();
            this.ctx.stroke();
        }
    }
    randPoin() {
        for (var i = 0; i < 30; i++) {
            this.ctx.beginPath();
            this.ctx.arc(this.rn(0, this.width), this.rn(0, this.height), 2, 0, 2 * Math.PI);
            this.ctx.closePath();
            this.ctx.fillStyle = this.randColor(150, 200);
            this.ctx.fill();
        }
    }
    rn(min, max) {
        return parseInt(Math.random() * (max - min) + min);
    }
}

export const getMenuByPath = (menus, mpath) => {
    if(menus){
        for(let i=0; i< menus.length; i++){
            if(menus[i].path == mpath){
                return menus[i];
            }else if(menus[i].childMenu && menus[i].childMenu.length){
                let getMenu = getMenuByPath(menus[i].childMenu, mpath);
                if(getMenu){
                    return getMenu;
                }
            }
        }
    }
}
export const getParentsByPath = (menus, mpath) => {
    if(menus){
        for(let i=0; i< menus.length; i++){
            if(menus[i].path == mpath){
                return [menus[i]];
            }else if(menus[i].childMenu && menus[i].childMenu.length){
                let getMenu = getParentsByPath(menus[i].childMenu, mpath);
                if(getMenu){
                    return getMenu.concat(menus[i]);
                }
            }
        }
    }
}
